package com.sunkingdom.core.login;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/login")
public class OAuthController {
	private static final String OAUTH_QQ_LOGIN_PAGE = "/login/oauth-qq";
	private static final String OAUTH_DEMO = "/login/demo";

	@RequestMapping(value = "/oauth/tencent", method = RequestMethod.GET)
	public String toOAuthPage(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		String lastVisit = request.getHeader("referer");
		request.setAttribute("appid", "1103693980");
		request.setAttribute("appkey", "lq0wc3nLcz37Av9K");
		request.setAttribute("redirectUri",
				"http://www.sunkingdom.com:8080/qq/demo/spring");
		request.setAttribute("lastVisit", lastVisit);
		System.out.println(lastVisit);
		return OAUTH_QQ_LOGIN_PAGE;
	}

	@RequestMapping(value = "validate", method = RequestMethod.POST)
	public void demoLoginValidation(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String lastVisit = request.getParameter("lastVisit");
		System.out.println("login succeed, from " + lastVisit);
		response.sendRedirect(lastVisit);
	}

	@RequestMapping(value = "/demo", method = RequestMethod.GET)
	public String demoToOAuthPage() {
		return OAUTH_DEMO;
	}
}
